import { createRouter, createWebHistory } from 'vue-router'
import LoginPage from '../views/Login.vue'
import RegisterPage from '../views/Register.vue'
import ForgotPasswordPage from '../views/ForgotPassword.vue'
import HomePage from '../views/Home.vue'
import FriendRequests from '@/views/FriendRequests.vue'
import VideoView from '@/views/VideoView.vue'
import AiChatPage from '@/views/AiChat.vue'

const routes = [
  {
    path: '/',
    name: 'login',
    component: LoginPage
  },
  {
    path: '/login',
    redirect: '/'
  },
  {
    path: '/register',
    name: 'register',
    component: RegisterPage
  },
  {
    path: '/forgot-password',
    name: 'forgotPassword',
    component: ForgotPasswordPage
  },
  {
    path: '/home',
    name: 'home',
    component: HomePage,
    meta: { requiresAuth: true }
  },
  {
    path: '/friend-requests',
    name: 'FriendRequests',
    component: FriendRequests,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/video',
    name: 'video',
    component: VideoView,
    meta: { requiresAuth: true }
  },
  {
    path: '/ai-chat',
    name: 'aiChat',
    component: AiChatPage,
    meta: { requiresAuth: true }
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 全局路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  
  // 只有访问需要认证的页面时才检查登录状态
  if (to.meta.requiresAuth) {
    // 如果没有token，重定向到登录页
    if (!token) {
      next('/')
    } else {
      next()
    }
  } else {
    // 不需要验证的页面直接通过
    next()
  }
})

export default router 